(function (window) {
    s = {};
    /**
     * @yangyuan		打开win
     *
     * @param {Object} name 目标win名
     * @param {Object} url  目标win的url
     * @param {Object} other 其他属性
     */
    s.openWin = function (name, url, other) {
        var data = {
            name: name,
            url: url,
            vScrollBarEnabled: false,
            reload: true,
            pageParam: {
                win: api.winName
            }
        };
        $c.jsonAppend(data, other);
        api.openWin(data);

    };

    /**
     * @yangyuan    关闭win
     *
     * @param {Object} name 关闭目标的name
     */
    s.closeWin = function (name) {
        var data = {};
        if (name) {
            data.name = name;
        };
        api.closeWin(data);
    };

    /**
     * @yangyuan  打开frame
     *
     * @param {String}	name	要打开的frame名称
     * @param {String}	url		要打开的frame路径
     * @param {Element}	top		上边距来源元素
     * @param {Element}	bottom	下边距来源元素
     * @param {Object}	other	打开frame的其他属性
     */
    s.openFrame = function (name, url, top, bottom, other) {
        var h = 'auto',
            t = 0,
            b = 0;
        if ($api.isElement(top)) {
            $api.fixIos7Bar(top);
            t = $api.offset(top).h
        } else {
            t = top;
        };
        if ($api.isElement(bottom)) {
            if (bottom != 0 && bottom) {
                b = $api.offset(bottom).h
                h = api.winHeight - t - b;
            };
        }
        if (top !== undefined && bottom !== undefined) {
            b = bottom;
            h = api.winHeight - t - b;
        }

        var data = {
            name: name,
            url: url,
            rect: {
                x: 0,
                y: t,
                w: 'auto',
                h: h
            },
            reload: true,
            bounces: false,
        };
        $c.jsonAppend(data, other);
        api.openFrame(data);
    };

    /**
     *
     * @yangyuan          打开frame组
     *
     * @param {Object} name   打开frame组名称
     * @param {Object} frames	打开frame页面集合
     * @param {Object} top			上边距来源元素
     * @param {Object} callback		回调
     * @param {Object} bottom		上边距来源元素
     */
    s.openFrameGroup = function (name, frames, top, callback, bottom) {
        var h = 'auto',
            t = 0,
            b = 0;
        if (top) {
            t = top
        };

        if (bottom != 0 && bottom) {
            b = bottom
            h = api.winHeight - t - b;
        }
        var data = {
            name: name,
            rect: {
                x: 0,
                y: t,
                w: 'auto',
                h: h
            },
            scrollEnabled: true,
            index: 0,
            preload: 0,
            frames: frames
        };
        api.openFrameGroup(data, function (ret) {
            if (callback) callback(ret);
        });
    };


    s.dAlert = function (title, content, LText, RText, cb) {
        dialogBox.alert({
            texts: {
                title: title ,
                content: content,
                leftBtnTitle: LText || "",
                rightBtnTitle: RText || ""
            },
            styles: {
                bg: '#fff',
                corner: 6,
                w: 260,
                title: {
                    marginT: 20,
                    icon: 'widget://image/gou.png',
                    iconSize: 32,
                    titleSize: 18,
                    titleColor: '#000'
                },
                content: {
                    color: '#000',
                    size: 16
                },
                left: {
                    marginB: 15,
                    marginL: 40,
                    w: 40,
                    h: 35,
                    corner: 2,
                    color: "#000",
                    bg: '#fff',
                    size: 16
                },
                right: {
                    marginB: 15,
                    marginL: 88,
                    w: 40,
                    h: 35,
                    corner: 2,
                    color: "#000",
                    bg: '#fff',
                    size: 16
                }
            },
        }, function (ret) {
            if (ret.eventType == 'left') {
                cb("L")
                if(LText != ""){
                    dialogBox.close({
                        dialogName: 'alert'
                    });
                }
            } else {
                cb("R")
                if(RText != ""){
                    dialogBox.close({
                        dialogName: 'alert'
                    });
                }
            }
        });
    }

    /**
     * @yangyuan		json数据追加
     *
     * @param{Object}	dest	目标json
     * @param{Object}	src		追加的json数据
     */
    s.jsonAppend = function (dest, src) {
        for (key in src) {
            if (!$c.isEmpty(dest[key])) {
                if (typeof dest[key] == "object") {
                    $c.jsonAppend(dest[key], src[key]);
                    continue;
                }
            }
            dest[key] = src[key];
        }
    };

    /**
     * 判空
     *
     * @param {Object} value
     */
    s.isEmpty = function isEmpty(value) {
        if (value == null || value == "" || value == "undefined" || value == undefined || value == "null" || value == "(null)" || value == 'NULL' || typeof (value) == 'undefined' || value == 'none') {
            return true;
        } else {
            value = value + "";
            value = value.replace(/\s/g, "");
            if (value == "") {
                return true;
            }
            return false;
        };
    };

    /**
     * 加载动画
     *
     * @param {String} title
     */
    s.showLoading = function (title) {
        api.showProgress({
            title: title + "...",
            text: '请稍等...'
        });
    };

    /**
     * 隐藏加载动画
     */
    s.hideLoading = function () {
        api.hideProgress();
    };

    /**
     * 弹出提示框
     *
     * @param {Object} msg
     */
    s.toast = function (msg) {
        api.toast({
            msg: msg,
            duration: 2000,
            location: 'bottom'
        });
    };

    /**
     * 网络数据请求
     *
     * @param {Object} json
     */
    s.getHttpData = function (json,type) {
        //检查网络连接状况
        if (api.connectionType == "none") {
            $c.toast("请检查网络连接");
            api.hideProgress();
            api.refreshHeaderLoadDone();
            return;
        };    
        // console.log(JSON.stringify(json));
        //ajax 默认参数
        var data = get_AJAX_PARAM();
        if(type){
            data.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        }
        
        //判断是否存在url，否则启用备用url
        if (!json.hasOwnProperty("url")) {
            $c.toast('url不能为空！');
            return;
        } else {
            //如果url含有http请求头则 不进行拼接
            if(json.url.includes("http") || json.url.includes("https")){
                data["url"] = json.url; //拼接url
            } else {
                data["url"] = AJAX_URL_HEAD + json.url; //拼接url
            }
        };


        //values
        if (json.hasOwnProperty("values")) {
            var values = json["values"];
            data["data"]["values"] = json.values;
        };
        //判断是否有上传文件
        if (json.hasOwnProperty("files")) {
            data['data']['files'] = json.files;
        };
        //判断是否配置请求类型
        if (json.hasOwnProperty("method")) {
            data['method'] = json.method;
        };
        //判断是否缓存
        if (!json.hasOwnProperty("cache")) {
            json.cache = false;
        };

        //判断是否返回全部数据
        if (json.hasOwnProperty("returnAll")) {
            data.returnAll = true;
        };

        //是否显示进度条
        if (json.hasOwnProperty("loading")) {
            $c.showLoading("加载中");
        };

        _ajax();

        function _ajax() {
            api.ajax(data, function (ret, err) {
                api.hideProgress();
                if (json.hasOwnProperty("loading")) {
                    $c.hideLoading();
                };
                $c.log("param:" + $api.jsonToStr(data));
                $c.log("url:" + data['url']);
                if (ret) {
                    if(ret.code == "1" && ret.msg == "401"){
                        api.alert({
                        title:"提示",
                            msg:"您还未登录，请进行登录"
                        },function(){
                            $c.openWin("login","widget://login.html");                        
                        })
                    }
                    $c.log("result:" + $api.jsonToStr(ret));
                    if (json.hasOwnProperty("callback")) {
                        json.callback(ret);
                    };
                } else {
                    $c.log("result:" + $api.jsonToStr(err));
                    $c.toast("连接服务器失败！");
                    api.hideProgress();
                }
            })
        };
    };

    /**
     * 图片缓存
     */
    s.imageCache = function () {
        var imgs = $api.domAll(".image-cache");
        for (var i = 0; i < imgs.length; i++) {
            var imageUrl = $api.attr(imgs[i], "image-cache");
            api.imageCache({
                url: imageUrl
            }, function (ret, err) {
                var url = ret.url;
                $c.log("缓存成功");
                $api.attr(imgs[i], "src", url);
            });
        };
    };

    /**
     * 打印日志
     *
     * @param {Object} object
     */
    s.log = function (name, object) {
        if (typeof name !== 'string') {
            object = name;
            name = null;
        }
        if (DEBUG) {
            var myDate = new Date();
            currentTime = myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
            if (typeof (object) == "string" || typeof (object) == "number" || typeof (object) == "boolean") {
                if (!$c.isEmpty(name)) {
                    console.log(currentTime + ": " + name + "-----" + object);
                } else {
                    console.log(currentTime + "-----" + object);
                }
            } else if (typeof (object) == "object") {
                if (!$c.isEmpty(name)) {
                    console.log(currentTime + ": " + name + "-----" + $api.jsonToStr(object));
                } else {
                    console.log(currentTime + "-----" + $api.jsonToStr(object));
                }
            };
        }
    };

    /**
     * 对话框日志
     *
     * @param {Object} object
     */
    s.alert = function (object) {
        if (DEBUG) {
            if (typeof (object) == "string" || typeof (object) == "number" || typeof (object) == "boolean") {
                alert(object);
            } else if (typeof (object) == "object") {
                alert($api.jsonToStr(object));
            };
        }
    }

    window.$c = s;
})(window);